import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.addFirst(2);
        linkedList.addFirst(1);
        linkedList.addLast(3);
        linkedList.addLast(4);
        linkedList.display();
        System.out.println();

        linkedList.addFirst(1);
        linkedList.remove(1);
        linkedList.remove(4);
        linkedList.display();
        System.out.println();

        linkedList.addFirst(1);
        linkedList.addFirst(1);
        linkedList.addFirst(1);
        linkedList.addFirst(1);
        linkedList.addIndex(7,1);
        linkedList.addIndex(18,1);

        linkedList.display();
        System.out.println();
        linkedList.removeAllKey(1);
        linkedList.display();
        System.out.println();
    }

    public void clear(){
        ListNode cur = head;
        while(cur != null){
            ListNode next = cur.next;
            cur.pre = null;
            cur.next = null;
            cur = next;
        }
        head = null;
        last = null;
    }
}
public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[10];
    }

    public void push(int val) {
        if (isFull()) {
            this.elem = Arrays.copyOf(elem, 2 * elem.length);
        }
        elem[usedSize++] = val;
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    public int pop() throws IndexIllegalException{
        try {
            if (isEmpty()) {
                throw new IndexIllegalException("栈为空");
            }
        }catch (IndexIllegalException e){
            e.printStackTrace();
        }
        return elem[--usedSize];
    }

    //获取栈顶元素 但是不删除
    public int peek() throws IndexIllegalException{
        try {
            if (isEmpty()) {
                throw new IndexIllegalException("栈为空");
            }
        }catch (IndexIllegalException e){
            e.printStackTrace();
        }
        return elem[usedSize - 1];
    }

    public boolean isEmpty() {

        return usedSize == 0;
    }

    public boolean IsPopOrder(int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(stack.peek() != popV[j]){
                stack.pop();
                j++;
            }
        }
        if(j==popV.length && stack.isEmpty()){
            return true;
        }
        return false;
    }
}